﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace Ict.DataAccess.Configuration
{
    /// <summary>
    /// Web.config里的数据访问设置节, 见以下例子
    /// <configSections>
    ///    <section name="DataAccessSession" type="Ict.DataAccess.Configuration.DataConfigSection, Ict.DataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
    /// </configSections>
    /// <DataAccessSession connectionName="connectionString">
    ///    <dataAccessAssembly assemblyName="Ict.DataAccess.SqlServer" executorClass="Ict.DataAccess.SqlServer.SqlExecutor" dmlBuilderClass="Ict.DataAccess.SqlServer.SqlDmlBuilder"/>
    /// </DataAccessSession>
    /// </summary>
    public class DataConfigSection : ConfigurationSection
    {
        public DataConfigSection()
        {
        }

        /// <summary>
        /// 获取或设置数据库连接字符串设置的名称
        /// </summary>
        [ConfigurationProperty("connectionName", DefaultValue = "connectionString", IsRequired = true)]
        public string ConnectionName
        {
            get
            {
                return (string)this["connectionName"];
            }
            set
            {
                this["connectionName"] = value;
            }
        }

        /// <summary>
        /// 获取或设置数据访问设置节里DataAccess实现类的设置
        /// </summary>
        [ConfigurationProperty("dataAccessAssembly", IsRequired = true)]
        public DataAccessAssemblyElement DataAccessAssembly
        {
            get
            {
                return this["dataAccessAssembly"] as DataAccessAssemblyElement;
                //DataAccessAssemblyElement asm = this["dataAccessAssembly"] as DataAccessAssemblyElement;
                //if (asm != null)
                //    return asm;
                //else
                //    return new DataAccessAssemblyElement();
            }
            set
            {
                this["dataAccessAssembly"] = value;
            }
        }
    }

    /// <summary>
    /// 数据访问设置节里DataAccess实现类的设置
    /// </summary>
    public class DataAccessAssemblyElement : ConfigurationElement
    {
        /// <summary>
        /// 获取或设置Assembly的名称
        /// </summary>
        [ConfigurationProperty("assemblyName", DefaultValue = "Ict.DataAccess.SqlServer", IsRequired = true)]
        public string AssemblyName
        {
            get { return (string)this["assemblyName"]; }
            set { this["assemblyName"] = value; }
        }

        /// <summary>
        /// 获取或设置Executor类的名称
        /// </summary>
        [ConfigurationProperty("executorClass", DefaultValue = "Ict.DataAccess.SqlServer.SqlExecutor", IsRequired = true)]
        public string ExecutorClass
        {
            get { return (string)this["executorClass"]; }
            set { this["executorClass"] = value; }
        }

        /// <summary>
        /// 获取或设置DmlBuilder类的名称
        /// </summary>
        [ConfigurationProperty("dmlBuilderClass", DefaultValue = "Ict.DataAccess.SqlServer.SqlDmlBuilder")]
        public string DmlBuilderClass
        {
            get { return (string)this["dmlBuilderClass"]; }
            set { this["dmlBuilderClass"] = value; }
        }

        [ConfigurationProperty("dataContextClass", DefaultValue = "Ict.DataAccess.SqlServer.SqlDataContext")]
        public string DataContextClass
        {
            get { return (string)this["dataContextClass"]; }
            set { this["dataContextClass"] = value; }
        }
    }
}
